<?php
include("../../tools/session.php");
include("../../tools/controls.php");
include("../../tools/pub.php");

// 如果登录失败、无权限访问，那么采取相应措施
if (!($is_login_success === true)) { session_hop_page("../index.php"); exit(); }
if (!power_check(0)) { include("tpl-inc-wp.php"); $tpl->display($oa_tpl_path . "/no-power.html"); exit(); }

/*-- 以下是实际内容 --*/
$page_status = $_POST["page_status"];
$per_page = 10;

include("../../tools/mysql.php");
db_connect();

if (!$page_status)
{
	include("tpl-inc-wp.php");
    
	// $sql = "SELECT ceil(count(id)/" . $per_page . ") num FROM user";
	$sql = "SELECT count(id) num FROM user";
	$re = db_query_once($sql);
		
	// $tpl->assign("page_num", $re["num"]);
	$tpl->assign("wholepage", $re["num"]);
	$tpl->display($oa_tpl_path . "/default/del_person.html");
}
elseif ($page_status == 1)
{
    $id = $_POST["id"];
    $del = $_POST["del"];
    $sql = "UPDATE user SET deleted=" . $del . " WHERE id='" . $id . "'";
    // echo (db_exec($sql)) ? "success!" : "failed!";
    $log_str = $del
    			? array("删除员工（ID: " . $id . "）成功！", "删除员工（ID: " . $id . "）失败！")
    			: array("恢复员工（ID: " . $id . "）成功！", "恢复员工（ID: " . $id . "）失败！");
    echo check_status_write_to_log(db_exec($sql), $log_str, FALSE);
}
elseif ($page_status == 2)
{
    	header("content-type: text/xml");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	echo "<people>\n";

	$page = $_POST["page"];
	$page = (!$page) ? 1 : $page;

	$page_min = $per_page * ($page - 1); 
	$sql = "SELECT id, username, register_time, deleted FROM user ORDER BY register_time DESC LIMIT " . $page_min . "," . $per_page;
	$sql_whole = "SELECT count(id) num FROM user";

	$re = db_query($sql);
	$num = count($re);
	$whole = db_query_once($sql_whole);
    
	$wholepage = ceil($whole["num"] / $per_page);

	echo "<perpage>" . $per_page . "</perpage>\n";
	echo "<wholepage>" . $wholepage . "</wholepage>\n";
	
	$str = "";
	foreach ($re as $v)
	{
	    $str .= "<item>\n";
	    $str .= "<id>" . $v["id"] . "</id>\n";
	    $str .= "<username>" . $v["username"] . "</username>\n";
	    $str .= "<register_time>" . $v["register_time"] . "</register_time>";
	    $str .= "<deleted>" . $v["deleted"] . "</deleted>";
	    $str .= "</item>\n";
	}
	
	echo $str;
	
	echo "</people>\n";
}
?>

